package com.example.filecabinet.controller;

import com.example.filecabinet.constant.RecordConstant;
import com.example.filecabinet.dao.Lattice_infoMapper;
import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Lattice_infoExample;
import com.example.filecabinet.service.Lattice_infoService;
import com.example.filecabinet.util.AutoIdFactory;
import com.example.filecabinet.util.QueryFreeFells;
import com.thfdcsoft.framework.common.file.FileFactory;
import com.thfdcsoft.framework.common.http.dto.BaseHttpRspn;
import com.thfdcsoft.framework.common.json.jackson.JacksonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;

@Controller
public class LatticeController {

    @Value("${logging.file}")
    private String logPath;

    @Autowired
    Lattice_infoService lattice_infoService;
    @Autowired
    Lattice_infoMapper lattice_infoMapper;

    private static Logger logger = LoggerFactory.getLogger(LatticeController.class);

    /**
     * @return boolean
     * @Author chengconghui
     * @Description //TODO 查询是否有空格子
     * @Date 15:16 2020/3/24
     * @Param deviceNumber
     **/
    @RequestMapping(value = "selectFreeFellNumber", method = RequestMethod.POST)
    @ResponseBody
    public String selectFreeFellNumber(@RequestBody String deviceNumber) {
        logger.info("进入selectFreeFellNumber方法,查询是否有空格子");
        logger.info("入参deviceNumber：" + deviceNumber);
        String fileUrl = logPath + AutoIdFactory.getAutoId() + ".txt";
        File recordFile = new File(fileUrl);
        FileFactory.writeTXTFile(recordFile, "进入selectFreeFellNumber方法,查询是否有空格子");
        FileFactory.writeTXTFile(recordFile, "入参deviceNumber：" + deviceNumber);
        BaseHttpRspn result = new BaseHttpRspn(false);
        boolean flag = QueryFreeFells.getFreeFellNumber(deviceNumber);
        result.setResult(flag);
        logger.info("返回结果flag：" + flag);
        FileFactory.writeTXTFile(recordFile, "查询格子返回数据：" + JacksonFactory.writeJson(result));
        return JacksonFactory.writeJson(result);
    }

    /**
     * @return Lattice_info
     * @Author chengconghui
     * @Description //TODO 分配格号
     * @Date 15:23 2020/3/24
     * @Param deviceNumber
     **/
    @RequestMapping(value = "getLatticeNumber", method = RequestMethod.POST)
    @ResponseBody
    public String getLatticeNumber(@RequestBody String deviceNumber) {
        BaseHttpRspn result = new BaseHttpRspn(false);
        logger.info("QueryFreeFells.getLatticeNumber(deviceNumber)");
        Lattice_info lattice_info = QueryFreeFells.getLatticeNumber(deviceNumber);
        System.out.println(lattice_info.toString());
        if (lattice_info != null) {
            result.setResult(true);
            result.setRespJson(JacksonFactory.writeJson(lattice_info));
        }
        return JacksonFactory.writeJson(result);
    }

    /**
     * @return string
     * @Author chengconghui
     * @Description //TODO 更新格子状态
     * @Date 15:34 2020/3/24
     * @Param lattice_info
     **/
    @RequestMapping(value = "updateLattice", method = RequestMethod.POST)
    @ResponseBody
    public String updateLattice(@RequestBody Lattice_info lattice_info) {
        BaseHttpRspn result = new BaseHttpRspn(false);
        result.setRespMsg("更新失败");
        int count = lattice_infoService.updateByPrimaryKeySelective(lattice_info);
        if (count != 0) {
            result.setRespMsg("更新成功");
            result.setResult(true);
        }
        return JacksonFactory.writeJson(result);
    }

    /**
     * @return
     * @Author chengconghui
     * @Description //TODO 工作人员查询待取业务
     * @Date 14:34 2020/3/26
     * @Param
     **/
    @RequestMapping(value = "selectEmpTakeLattice", method = RequestMethod.POST)
    @ResponseBody
    public String selectEmpTakeLattice(@RequestBody String employeeId) {
        logger.info("进入工作人员查询待取业务方法：selectEmpTakeLattice()");
        logger.info("入参employeeId：" + employeeId);
        BaseHttpRspn result = new BaseHttpRspn(false);
        result.setRespMsg("无待取格子");
        Lattice_info lattice_info = new Lattice_info();
        lattice_info.setLatticeLdleStatus(RecordConstant.LATTIC_LDLE_STATUS.USING);
        lattice_info.setLatticeSwithStatus(RecordConstant.LATTICE_SWITH_STATUS.ERROR);
        lattice_info.setLatticeOperator(RecordConstant.LATTICE_OPERATOR.CUSTOMER);
        List<Lattice_info> lattice_infos = lattice_infoService.selectEmpTakeLattice(employeeId, lattice_info);
        logger.info("查询结果lattice_infos.size()：查询到" + lattice_infos.size() + "条数据！");
        if (lattice_infos.size() != 0) {
            result.setRespMsg("返回待取格子");
            result.setRespJson(JacksonFactory.writeJson(lattice_infos));
            result.setResult(true);
        }
        return JacksonFactory.writeJson(result);
    }

    /**
     * @return string
     * @Author chengconghui
     * @Description //TODO 普通用户取件&&山东用户取件&&定位开门&&故障开门
     * @Date 09:30 2020/3/27
     * @Param lattice_info
     **/
    @RequestMapping(value = "selectUserTakeLattice", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserTakeLattice(@RequestBody Lattice_info lattice_info) {
        logger.info("进入selectUserTakeLattice()方法");
        BaseHttpRspn result = new BaseHttpRspn(false);
        result.setRespMsg("无待取格子");
        Lattice_infoExample lattice_infoExample = new Lattice_infoExample();
        Lattice_infoExample.Criteria criteria = lattice_infoExample.createCriteria();
        criteria.andDeviceNumberEqualTo(lattice_info.getDeviceNumber());
        //1.普通用户取需要身份证
        //2.山东用户要身份证和业务编号
        //1，2都需要原来的格子是工作人员操作的，即LatticeOperator=“00”
        //3.定位开门则只需要格子编号
        if (lattice_info.getLatticeOperator() != null) {
            logger.info("用户取件方法");
            logger.info("入参：");
            logger.info("原来的格子是工作人员操作latticeOperator：" + lattice_info.getLatticeOperator());
            //00，用户取必须原来的格子是工作人员操作的
            criteria.andLatticeOperatorEqualTo(lattice_info.getLatticeOperator());
            //用户只能取非故障文件
            criteria.andLatticeSwithStatusNotEqualTo(RecordConstant.LATTICE_SWITH_STATUS.ERROR);
        }
        //用户取件必须是非空格子latticeLdleStatus=01
        if (lattice_info.getLatticeLdleStatus() != null) {
            logger.info("用户取件必须是非空格子latticeLdleStatus：" + lattice_info.getLatticeLdleStatus());
            criteria.andLatticeLdleStatusEqualTo(lattice_info.getLatticeLdleStatus());
        }
        //身份证
        if (lattice_info.getLatticeRemarks1() != null) {
            logger.info("用户身份证latticeRemarks1：" + lattice_info.getLatticeRemarks1());
            criteria.andLatticeRemarks1EqualTo(lattice_info.getLatticeRemarks1());
        }
        //业务编号
        if (lattice_info.getBusinessNumber() != null) {
            logger.info("用户业务编号businessNumber：" + lattice_info.getBusinessNumber());
            criteria.andBusinessNumberEqualTo(lattice_info.getBusinessNumber());
        }
        //定位开门
        if (lattice_info.getLatticeNumber() != null) {
            logger.info("定位开门");
            logger.info("入参：");
            logger.info("格子编号latticeNumber：" + lattice_info.getLatticeNumber());
            criteria.andLatticeNumberEqualTo(lattice_info.getLatticeNumber());
        }
        //故障开门
        if (lattice_info.getLatticeSwithStatus() != null) {
            logger.info("故障文件");
            logger.info("入参：");
            logger.info("故障状态latticeSwithStatus：" + lattice_info.getLatticeSwithStatus());
            criteria.andLatticeNumberEqualTo(lattice_info.getLatticeNumber());
            criteria.andLatticeSwithStatusEqualTo(lattice_info.getLatticeSwithStatus());
        }
        List<Lattice_info> lattice_infos = lattice_infoService.selectByExample(lattice_infoExample);
        logger.info("查询结果lattice_infos.size()：查询到" + lattice_infos.size() + "条数据！");
        if (lattice_infos.size() != 0) {
            result.setRespMsg("返回待取格子");
            result.setRespJson(JacksonFactory.writeJson(lattice_infos));
            result.setResult(true);
        }
        return JacksonFactory.writeJson(result);
    }

    /**
     * @return
     * @Author chengconghui
     * @Description //TODO 逾期开门
     * @Date 10:51 2020/3/31
     * @Param
     **/
    @RequestMapping(value = "overdueOpening")
    @ResponseBody
    public String overdueOpening(@RequestBody String deviceNumber) {
        BaseHttpRspn result = new BaseHttpRspn(false);
        result.setRespMsg("无逾期文件");
        List<Lattice_info> lattice_infos = lattice_infoService.selectByDate(deviceNumber);
        if (lattice_infos.size() != 0) {
            result.setRespMsg("返回查询到的逾期文件格子");
            result.setRespJson(JacksonFactory.writeJson(lattice_infos));
            result.setResult(true);
        }
        return JacksonFactory.writeJson(result);
    }

    @RequestMapping("/selectOpenLattice")
    @ResponseBody
    public String selectOpenLattice(@RequestBody String deviceNumber) {
        BaseHttpRspn result = new BaseHttpRspn(false);
        result.setRespMsg("没有开启的柜门");
        List<Lattice_info> lattice_infos = lattice_infoService.selectByDate(deviceNumber);
        if (lattice_infos.size() != 0) {
            result.setRespMsg("查询到开启的柜门");
            result.setRespJson(JacksonFactory.writeJson(lattice_infos));
            result.setResult(true);
        }
        return JacksonFactory.writeJson(result);
    }

}
